package com.sat.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sat.goods.mapper.AlbumMapper;
import com.sat.goods.model.Album;
import com.sat.goods.service.AlbumService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AlbumServiceImpl extends ServiceImpl<AlbumMapper, Album> implements AlbumService {

    @Autowired
    private AlbumMapper albumMapper;

    @Override
    public List<Album> findList(Album album) {
        QueryWrapper<Album> queryWrapper = getAlbumQueryWrapper(album);
        return albumMapper.selectList(queryWrapper);
    }

    @Override
    public Page findPage(Integer page, Integer size) {
        Page<Album> albumPage = new Page<>(page, size);
        return albumMapper.selectPage(albumPage, null);
    }

    @Override
    public Page findPage(Integer page, Integer size, Album album) {
        Page<Album> albumPage = new Page<Album>(page, size);
        QueryWrapper<Album> albumQueryWrapper = getAlbumQueryWrapper(album);
        return albumMapper.selectPage(albumPage,albumQueryWrapper);
    }

    private QueryWrapper<Album> getAlbumQueryWrapper(Album album) {
        QueryWrapper<Album> queryWrapper = new QueryWrapper<>();
        if (album != null) {
            if (album.getTitle() != null && !"".equals(album.getTitle())) {
                queryWrapper.like("title", album.getTitle());
            }
        }
        return queryWrapper;
    }
}
